//
//  CurrentPage.swift
//  test1
//
//  Created by Apple on 2021/6/8.
//

/*
 参数：
 logo: 点击搜索前的页面的url
 url：点击搜索前的页面的title
 title：点击搜索前的页面的logo
 callBack(可选):使用的回调函数
 */

import SwiftUI

struct CurrentPage: View {
    @EnvironmentObject var modelData : HistoryListModelData
    var logo: Image
    var url: String
    var title: String
    var callBack : ViewCallBack?
    var body: some View {
        HStack{
            Button(action: {
                //callBack?.goToView(by: .IndexView, sender: self, with: [RequestArgumentKey.wd.rawValue:url])
                callBack?.goToView(to: .IndexView, from: .SearchView, intent: .Default, with: [.wd:url])
//                if let window = UIApplication.shared.windows.first
//                {
//                    window.rootViewController = UIHostingController(rootView: SearchResultPage(url: url).environmentObject(ModelData()))
//                    window.makeKeyAndVisible()
//                }
            }){
                HStack{
                    logo
                        .resizable()
                        .frame(width: 25, height: 25)
                        .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10 ))
                    VStack{
                        Text(title)
                            .font(.title3)
                            .frame(width: 130, height: 30, alignment: .leading)
                            .lineLimit(nil)
                            .foregroundColor(.black)
                        Text(url)
                            .frame(width: 130, height: 20, alignment: .leading)
                            .foregroundColor(.blue)
                            .lineLimit(nil)
                    }
                }
            }
            Spacer()
            Button(action: {
                print("share")
            }){
                Image("share")
                    .resizable()
                    .frame(width: 25, height: 25)
            }
            Button(action: {
                UIPasteboard.general.string = self.url
            }){
                Image("copy")
                    .resizable()
                    .frame(width: 25, height: 25)
                    .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 0 ))
            }
            Button(action: {
                modelData.Searchtext = self.url
            }){
                Image("edit")
                    .resizable()
                    .frame(width: 25, height: 25)
                    .padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10 ))
            }
        }
    }
}

struct CurrentPage_Previews: PreviewProvider {
    static var previews: some View {
        CurrentPage(logo: Image("baidu"),url: "https://www.baidu.com", title: "当前标签页title").environmentObject(HistoryListModelData())
    }
}
